package net.adetech.crj.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class MainServiceController {

    @Value("${url.inner_service}")
    private String innerServiceUrl;

    @Value("${url.coreweb}")
    private String corewebUrl;

    @Value("${devMode}")
    private boolean devMode;


    @RequestMapping(value = "/crj/callOut", name = "application/json")
    public String callOut(@RequestBody String input) {

        JSONObject jsonObject = JSONObject.parseObject(input);
        String data = jsonObject.getString("data");
        if ("heartbeat check".equals(data))
        {
            return "{result:'success'}";
        }
        String responseString = "{result:'failed'}";
        try {
            List<NameValuePair> paramList = new ArrayList<NameValuePair>();
            paramList.add(new BasicNameValuePair("data", data));
            HttpEntity reqEntity = new UrlEncodedFormEntity(paramList, "utf-8");
            HttpClient client = HttpClients.createDefault();
            HttpPost request = new HttpPost(corewebUrl);
            request.setEntity(reqEntity);
            //request.setHeader("Authorization","APPCODE f77530e7d1324c3bbd47c819aa33711c");
            HttpResponse response = client.execute(request);
            responseString = EntityUtils.toString(response.getEntity());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseString;
    }

    @RequestMapping(value = "/crj/callIn", name = "application/json")
    public String callIn(String data ) {




        String responseString = "{}";
        try {
            StringEntity entity = new StringEntity("{data:'"+data+"'}","UTF-8");
            entity.setContentType("application/json");

            HttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost(innerServiceUrl);
            post.setEntity(entity);

            if (!devMode) {
                post.setHeader("COLLAGEN-TIMEOUT", "30S");
                post.setHeader("COLLAGEN-DEBUG", "OFF");
                post.setHeader("Content-Type", "application/json");
                post.setHeader("COLLAGEN-OUT_PARAMETERS", "_ALL_");
                post.setHeader("COLLAGEN-SESSION_ID", "no-session");
                post.setHeader("COLLAGEN-REQUESTER_ID", "SD.YTGA.APP.GA.CRJZNYD");
                post.setHeader("COLLAGEN-PROXY_FLOW_ID", "SD.YTGA::01::FLOW_C3_CALL_RESTFUL_PROXY");
                post.setHeader("COLLAGEN-AUTHORIZE_ID", "2a03387cfe4245ce993df05ee3d10e5f");

            }
            //request.setHeader("Authorization","APPCODE f77530e7d1324c3bbd47c819aa33711c");

            HttpResponse response = client.execute(post);
            responseString = EntityUtils.toString(response.getEntity());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseString;
    }



}
